import java.util.Random;

public class Main {
    public static void main(String[] args) {

        // 定义一个大整数n，用于后续生成大规模的随机数数据集
        int n = 10000000;

        Random random = new Random();
        Integer[] testData = new Integer[n];
        for (int i = 0; i < n; i++) {
            testData[i] = random.nextInt(Integer.MAX_VALUE);
        }
        double time1 = testHeap(testData, false);
        System.out.println("Without heapify: "+time1+" s");
        double time2 = testHeap(testData, true);
        System.out.println("With heapify: "+time2+" s");

    }

    private static double testHeap(Integer [] testData, boolean isHeapify){
        long startTime = System.nanoTime();

        MaxHeap<Integer> maxHeap;
        if (isHeapify){
            maxHeap = new MaxHeap<>(testData);
        }else {
            maxHeap = new MaxHeap<>();
            for (int num:testData){
                maxHeap.add(num);
            }
        }

        // 初始化一个整型数组，用于存储最大堆中的数据，以便后续分析和检查
        int[] arr = new int[testData.length];
        // 从最大堆中逐个提取最大元素，并存储到数组中，测试最大堆的提取最大值的性能
        for (int i = 0; i < testData.length; i++) {
            arr[i] = maxHeap.extractMax();
        }
        for (int i = 1; i < testData.length; i++) {
            if (arr[i-1] < arr[i]){
                throw new IllegalArgumentException("Error");
            }
        }

        System.out.println("Test MaxHeap completed. ");

        long endTime = System.nanoTime();

        return (endTime - startTime) / 1000000000.0;
    }

}
